//approval.js
Page({
  data: {
    records: [],
    showStatusDialog: false,
    currentRecord: {
      _id: '',
      status: ''
    }
  },
  
  onLoad: function() {
    // 页面加载时获取申请记录
    this.getRecords();
  },
  
  onShow: function() {
    // 页面显示时刷新记录列表
    this.getRecords();
  },
  
  // 获取申请记录
  getRecords: function() {
    wx.showLoading({
      title: '加载中...',
    });
    
    const db = wx.cloud.database();
    db.collection('entry_records')
      .orderBy('createTime', 'desc') // 按提交时间倒序排列
      .get()
      .then(res => {
        wx.hideLoading();
        this.setData({
          records: res.data
        });
      })
      .catch(err => {
        console.error('获取记录失败', err);
        wx.hideLoading();
        wx.showToast({
          title: '加载失败，请重试',
          icon: 'none',
          duration: 2000
        });
      });
  },
  
  // 获取状态对应的样式类名
  getStatusClass: function(status) {
    switch(status) {
      case '已提交':
        return 'status-submitted';
      case '已审批':
        return 'status-approved';
      case '已入区':
        return 'status-entered';
      case '已拒绝':
        return 'status-rejected';
      default:
        return '';
    }
  },
  
  // 格式化时间
  formatTime: function(time) {
    if (!time) return '';
    
    const date = new Date(time);
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const hour = date.getHours();
    const minute = date.getMinutes();
    const second = date.getSeconds();
    
    return `${year}-${this.padZero(month)}-${this.padZero(day)} ${this.padZero(hour)}:${this.padZero(minute)}:${this.padZero(second)}`;
  },
  
  // 补零函数
  padZero: function(num) {
    return num < 10 ? '0' + num : num;
  },
  
  // 显示状态修改菜单
  showStatusMenu: function(e) {
    const id = e.currentTarget.dataset.id;
    const record = this.data.records.find(item => item._id === id);
    
    if (record) {
      this.setData({
        showStatusDialog: true,
        currentRecord: record
      });
    }
  },
  
  // 隐藏状态修改菜单
  hideStatusDialog: function() {
    this.setData({
      showStatusDialog: false
    });
  },
  
  // 阻止冒泡事件
  stopPropagation: function(e) {
    // 阻止事件冒泡，避免点击弹窗内容时关闭弹窗
  },
  
  // 更新状态
  updateStatus: function(e) {
    const newStatus = e.currentTarget.dataset.status;
    const recordId = this.data.currentRecord._id;
    
    if (newStatus === this.data.currentRecord.status) {
      wx.showToast({
        title: '状态未变更',
        icon: 'none',
        duration: 1500
      });
      return;
    }
    
    wx.showLoading({
      title: '更新中...',
    });
    
    const db = wx.cloud.database();
    db.collection('entry_records').doc(recordId).update({
      data: {
        status: newStatus,
        updateTime: db.serverDate()
      },
      success: res => {
        wx.hideLoading();
        
        // 更新本地数据
        const updatedRecords = this.data.records.map(item => {
          if (item._id === recordId) {
            return {
              ...item,
              status: newStatus
            };
          }
          return item;
        });
        
        this.setData({
          records: updatedRecords,
          currentRecord: {
            ...this.data.currentRecord,
            status: newStatus
          },
          showStatusDialog: false
        });
        
        wx.showToast({
          title: '状态更新成功',
          icon: 'success',
          duration: 2000
        });
      },
      fail: err => {
        console.error('更新状态失败', err);
        wx.hideLoading();
        wx.showToast({
          title: '更新失败，请重试',
          icon: 'none',
          duration: 2000
        });
      }
    });
  }
})